package com.cskaoyan.wordmemorize.controller.app;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dao.entity.UserNotRememberDO;
import com.cskaoyan.wordmemorize.dao.mapper.UserNotRememberMapper;
import com.cskaoyan.wordmemorize.dao.mapper.UserPrivilegeHistoryMapper;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.dto.app.*;
import com.cskaoyan.wordmemorize.request.AIReviewReadCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserFavoriteCommand;
import com.cskaoyan.wordmemorize.request.UserVocExampleSentenceCommand;
import com.cskaoyan.wordmemorize.service.UserPrivilegeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "移动端-用户特权模块")
@RestController
@RequestMapping("/user/priviledge")
public class UserPrivilegeController {
    @Autowired
    UserPrivilegeService userPrivilegeService;

    // 1.APP用户标识不想背
    @PostMapping("voc/non")
    public Result notRemember(Long vocId){
        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.notRemember(vocId,userId);
        return Result.ok();
    }


    // 2.APP用户取消不想背
    @PutMapping("voc/non/cancel")
    public Result cancelNotRemember(Long vocId){
        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.cancelNotRemember(vocId,userId);
        return Result.ok();
    }


    // 3.APP端添加用户自建例句
    @PostMapping("voc/example")
    public Result<UserExampleSentenceDTO> addUserVocSentence(@RequestBody UserVocExampleSentenceCommand command){
        Long userId = StpKit.USER.getLoginIdAsLong();
        UserExampleSentenceDTO data =  userPrivilegeService.addUserVocSentence(command, userId);
        return Result.ok(data);
    }


    // 4.APP端获取用户自建例句
    @GetMapping("voc/example/list")
    public Result getUserVocSentence(@RequestBody PageRequest pageRequest){
        Long userId = StpKit.USER.getLoginIdAsLong();
        PageDTO<DisplayUserVocSentenceDTO> userVocSentence = userPrivilegeService.getUserVocSentence(pageRequest, userId);
        return Result.ok(userVocSentence);
    }


    // 5.APP端更新用户自建例句
    @PutMapping("voc/example")
    public Result updateUserVocSentence(@RequestBody UserVocExampleSentenceCommand command){
        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.updateUserVocSentence(command,userId);
        return Result.ok();
    }



    // 6.APP端删除用户自建例句
    @DeleteMapping("voc/example/{id}")
    public Result deleteUserVocSentence(@PathVariable Long id){
        userPrivilegeService.deleteUserVocSentence(id);
        return Result.ok();
    }

    // 7.APP端单个添加收藏单词
    @PutMapping("**favorite")
    public Result addFavorite(@RequestBody UserFavoriteCommand command){
        Long userId = StpKit.USER.getLoginIdAsLong();
        try {
            userPrivilegeService.addFavorite(command, userId);
        }
        catch (Exception e){
            System.out.println("单词已收藏");
            return Result.fail("单词已收藏");
        }

        return Result.ok();
    }

    // 8.APP端取消收藏单词
    @PostMapping("favorite/cancel")
    public Result cancelFavorite(@RequestBody List<Long> ids){
        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.cancelFavorite(ids, userId);
        return Result.ok();
    }


    // 9.APP端批量添加收藏单词
    @PostMapping("favorite/bulk")
    public Result addFavoriteBulk(@RequestBody List<UserFavoriteCommand> userFavoriteCommand){
        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.addFavoriteBulk(userFavoriteCommand, userId);
        return Result.ok();
    }

    // 10.APP端获取单词收藏列表
    @GetMapping("favorite/list")
    public Result getUserFavoriteList(@RequestBody PageRequest pageRequest){
        Long userId = StpKit.USER.getLoginIdAsLong();
        PageDTO<UserFavoriteDTO> userFavoriteList = userPrivilegeService.getUserFavoriteList(pageRequest, userId);
        return Result.ok(userFavoriteList);
    }

    // 11.APP端获取词书进度***
    @GetMapping("current/{bookId}/statistics")
    public Result getBookProgress(@PathVariable Long bookId){
        Long userId = StpKit.USER.getLoginIdAsLong();
        UserVocBookStatisticsDTO selectedBookStatistics = userPrivilegeService.getSelectedBookStatistics(userId,bookId);
        return Result.ok(selectedBookStatistics);
    }

    // 12.APP端获取AI阅读模式风格以及选项**
    @PostMapping("review/ai/read/options")
    public Result getReviewAIOptions(){
        Long userId = StpKit.USER.getLoginIdAsLong();
        List<UserVocAISituationDTO> reviewAIOptions = userPrivilegeService.getAIReviewReadOptions();
        return Result.ok(reviewAIOptions);
    }

    // 13.APP端获取用户剩余免费AI阅读次数***
    @PostMapping("review/ai/read/free")
    public Result getReviewAIFree(){
        Long userId = StpKit.USER.getLoginIdAsLong();
        // TODO
        Integer reviewAIFree = userPrivilegeService.getUserVocAIReadRemainFreeCount(userId);
        return Result.ok(reviewAIFree);
    }

    // 14.APP端获取AI阅读结果****
    @PostMapping("review/ai/read")
    public Result getReviewAIRead(@RequestBody AIReviewReadCommand command){
        Long userId = StpKit.USER.getLoginIdAsLong();
        // TODO
        List<AIReviewReadDTO> aiReviewReadContent = userPrivilegeService.getAIReviewReadContent(command, userId);
        return Result.ok(aiReviewReadContent);
    }

}
